home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!news
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c++
- Subject: Re: operator<< precedence, cout, and weirdness
- Date: Sat, 13 Apr 1996 08:58:58 GMT
- Organization: Netcom
- Message-ID: <316f6811.209729505@nntp.ix.netcom.com>
- References: <1996Apr12.133813.78123@cc.usu.edu>
- NNTP-Posting-Host: ix-dc6-23.ix.netcom.com
- X-NETCOM-Date: Sat Apr 13 3:57:54 AM CDT 1996
- X-Newsreader: Forte Agent .99d/32.182
-
- pjh@gas.physics.usu.edu (Paul Hepworth) wrote:
-
- > I'm having a hard time figuring out the weird eval order
- > of these operator<< expressions:
- >
- > Here is the output from the prog below:
- > 2220
- > 2220
- >
- > It appears the i++/++i expressions are being evaluated right to
- > left instead of left to right.
- > I included a parenthesized version to explicitly specify
- > the order I think it should already be evaluated in, and it
- > gives the same result.
- >
- > What's the deal?!
- >
- > #include <stream.h>
- > int main()
- > {
- > int i=0;
- >
- > cout << i++ << i << ++i << i++ << endl;
- > cout << i << endl;
- >
- > i=0;
- > (((( cout << i++) << i) << ++i) << i++) << endl;
- > cout << i << endl;
- > }
- >
- > I think it should evaluate as follows:
- > i++, producing integer result
- > cout << above result, producing istream&
- > above result << i, producing istream&
- > ++i, producing integer
- > above istream& << above integer result, producing istream&
- > i++, producing integer
- > above istream& << above integer, producing istream&
- > above istream& << endl, producing istream&
- >
- > instead, it seems to evaluate the i++/++i expressions to temporaries,
- > then cout << tmp1 << tmp2....
- >
- > BTW, with numbers, it evaluates as expected:
- > 1 << 1 << 2 produces 8, not 16
-
- The deal is that C++ does not specify the order in which operands are
- evaluated except for a few operators. << is not one of these. The
- compiler is free to order the evaluations in any way it finds
- convenient.
-
- Parentheses affect the order of evaluation of operators, but not
- operands. Your parentheses don't matter here since the order of
- evaluation of the operators is the same as you specify. In either
- case, the order of evaluation of the operators will be
-
- cout << i++ << i << ++i << i++ << endl;
- 1 2 3 4 5
-
- But the operands may be evaluated in any order. consider
-
- cout << A << B;
-
- The compiler may
-
- 1. evaluate A
- 2. evaluate cout << A
- 3. evaluate B
- 4. evaluate (the result of 2) << B
-
- That's what you expect. But it may also
-
- 1. evaluate B
- 2. evaluate A
- 3. evaluate cout << A
- 4. evaluate (the result of 3) << (the result of 1)
-
- Actually it gets worse. Side-effects may take place any time between
- sequence points. Since << does not introduce a sequence point, even
- if the value of A is determined first, side-effects may not be done
- until after the evaluation of B.
-
- And even worse. The above is accurate in practice, but the language
- definition gives the compiler even more freedom. Modifying a variable
- twice between sequence points results in undefined behavior. Anything
- may happen in your example -- no result or output need even be
- produced.
-
-
- Michael M Rubenstein
-